package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TCaiminfo;
import com.zmht.beike.po.TCmonthcontent;
import com.zmht.beike.po.TCmonthinfo;
import com.zmht.specedu.vo.Teacher;
/**
 * 集体月主题内容action
 * @author Administrator
 *
 */
public class CmonthContentAction extends BaseAction {

	/* 初始化月主题内容添加页面
	 * (non-Javadoc)
	 * @see org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward initMonthContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession();
		Teacher teacher = (Teacher)session.getAttribute("current_user_session_key");
		Integer month = new Integer(request.getParameter("month"));
		String monthThemeName = "";
		
		//开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		if(null!=request.getParameter("pageSize")){
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		
		
		List<TCmonthcontent> list = this.getCmonthContentBiz().findCmonthContent(session.getId(), teacher.getTeacherId(), month, (index-1)*pageSize, pageSize);
		int total = this.getCmonthContentBiz().findCmonthContentTotal(session.getId(), teacher.getTeacherId(), month, (index-1)*pageSize, pageSize);
		
		
		request.setAttribute("currPage", index); 
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("monthcontent", list);
		
		request.setAttribute("month", month+"");
		
		if(list!=null&&list.size()>0){monthThemeName = list.get(0).getMonthThemeName();}
		request.setAttribute("monthThemeName",monthThemeName);
		return mapping.findForward("yuezhuti_content");
	}
	/**
	 * 添加集体月目标内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addMonthContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession();
		String sessionID = session.getId();
		Teacher teacher = (Teacher)session.getAttribute("current_user_session_key");
		TCaiminfo caiminfo = (TCaiminfo)request.getSession().getAttribute("caiminfo");
		if(null==caiminfo){
			String targetId = request.getParameter("targetId");
			caiminfo = this.getCaiminfoBiz().findById(new Integer(targetId));
		}
		Integer month = new Integer(request.getParameter("month"));
		String cmonthContent = request.getParameter("cmonthContent");
		String monthThemeName = request.getParameter("monthThemeName");
		
		
		TCmonthcontent monthContent = new TCmonthcontent();
		monthContent.setSessionID(sessionID);
		monthContent.setTeacherId(teacher.getTeacherId());
		monthContent.setMonth(month);
		monthContent.setCmonthContent(cmonthContent);
		monthContent.setMonthThemeName(monthThemeName);
		monthContent.setCaimId(caiminfo.getId());
		//monthContent.setCmonthId(monthinfo.getId());
		monthContent.setCareaId(0);
		//编辑时使用
		String areaId=request.getParameter("areaId");
		if(null!=areaId&&!"null".equals(areaId)&&!"".equals(areaId)){
			monthContent.setCareaId(new Integer(areaId));
		}
		monthContent.setTerm(caiminfo.getTerm());
		
		
		this.getCmonthContentBiz().saveCmontContent(monthContent);
		
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			
			return new ActionForward("/beike/cmonthcontent.do?method=initMonthContentEdit&month="+month+"&targetId="+targetId+"&areaId="+areaId);
		}
		return new ActionForward("/beike/cmonthcontent.do?method=initMonthContent&month="+month);
	}
	/**
	 * 删除月主题内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delMonthContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		//String sessionID = session.getId();
		//Teacher teacher = (Teacher)session.getAttribute("current_user_session_key");
	
		String ctid = request.getParameter("ctid");
		String month = request.getParameter("month");
		
		this.getCmonthContentBiz().delCmontContent(new Integer(ctid));
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			String areaId=request.getParameter("areaId");
			return new ActionForward("/beike/cmonthcontent.do?method=initMonthContentEdit&month="+month+"&targetId="+targetId+"&areaId="+areaId);
		}
		
		return new ActionForward("/beike/cmonthcontent.do?method=initMonthContent&month="+month);
	}
	/**
	 * 更新月主题内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateMonthContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cid = request.getParameter("cid");
		String month = request.getParameter("month");
		String cmonthContent = request.getParameter("cmonthContent");
		
		TCmonthcontent content = this.getCmonthContentBiz().findById(new Integer(cid));
		if(null!=content){
			content.setCmonthContent(cmonthContent);
			this.getCmonthContentBiz().updateCmontContent(content);
		}
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			String areaId = request.getParameter("areaId");
			return new ActionForward("/beike/cmonthcontent.do?method=initMonthContentEdit&month="+month+"&targetId="+targetId+"&areaId="+areaId);
		}
		
		return new ActionForward("/beike/cmonthcontent.do?method=initMonthContent&month="+month);
	}
	/**
	 * 当编辑目标时 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initMonthContentEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession();
		Teacher teacher = (Teacher)session.getAttribute("current_user_session_key");
		Integer month = new Integer(request.getParameter("month"));
		String targetId = request.getParameter("targetId");
		String areaId = request.getParameter("areaId");
		
		
		Integer careaId = 0;
		if(null!=areaId&&!"".equals(areaId)){careaId = new Integer(areaId);}
		
		
		String monthThemeName = "";
		
		
		
		//开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		if(null!=request.getParameter("pageSize")){
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		
		
		//List<TCmonthcontent> list = this.getCmonthContentBiz().findCmonthContent(session.getId(), teacher.getTeacherId(), month, (index-1)*pageSize, pageSize);
		//int total = this.getCmonthContentBiz().findCmonthContentTotal(session.getId(), teacher.getTeacherId(), month, (index-1)*pageSize, pageSize);
		List<TCmonthcontent> list = this.getCmonthContentBiz().findCmonthContentEdit(new Integer(targetId),teacher.getTeacherId(),careaId, month, (index-1)*pageSize, pageSize);
		int total = this.getCmonthContentBiz().findCmonthContentEditTotal(new Integer(targetId), teacher.getTeacherId(),careaId, month);
	
		request.setAttribute("currPage", index); 
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("monthcontent", list);
		
		request.setAttribute("month", month+"");
		
		if(list!=null&&list.size()>0){monthThemeName = list.get(0).getMonthThemeName();}
		request.setAttribute("monthThemeName",monthThemeName);
		
		
		request.setAttribute("targetId", targetId);
		request.setAttribute("areaId", areaId);
		request.setAttribute("edit", "1");
		return mapping.findForward("yuezhuti_content");
	}
}
